import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { StatusBar } from '@ionic-native/status-bar';
import { Platform, AlertController, Loading, LoadingController, ToastController } from 'ionic-angular';
import { Toast } from '@ionic-native/toast';
import { Network } from '@ionic-native/network';
import { AppVersion } from '@ionic-native/app-version';
import { InAppBrowser } from '@ionic-native/in-app-browser';

/*
  Generated class for the NativeProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class NativeProvider {
  private loading: Loading;
  public  timer:number = 0;
  public  refreshTokenInterval;
  public  timerInterval;
  constructor(public http: HttpClient, private statusBar: StatusBar, private platform: Platform, private alertCtrl: AlertController, private loadingCtrl: LoadingController,
    private toastCtrl: ToastController, private toast: Toast,private network:Network,private appversion:AppVersion,
    private inAppBrowser: InAppBrowser) {
    console.log('Hello NativeProvider Provider');
  }

  isMobile(): boolean {
    return this.platform.is('mobile') && !this.platform.is('mobileweb');
  }

  setStatusStyle(){
    if(this.isMobile){
      this.statusBar.overlaysWebView(false);//web view 覆盖statusbar
      this.statusBar.styleLightContent(); //设置字体为浅色气体
      // this.statusBar.styleDefault(); // 使用黑色字体
      this.statusBar.backgroundColorByHexString('#488aff'); // 3261b3 设置背景颜色为蓝色
    }
  }

 /**
   * 只有一个确定按钮的弹出框.
   * 如果已经打开则不再打开
   */
  alert = (() => {
    let isExist = false;
    return (title: string, subTitle = '', message = '', callBackFun = null): void => {
      if (!isExist) {
        isExist = true;
        this.alertCtrl.create({
          title,
          subTitle,
          message,
          cssClass: 'alert-zIndex-highest',
          buttons: [{
            text: '确定', handler: () => {
              isExist = false;
              callBackFun && callBackFun();
            }
          }],
          enableBackdropDismiss: false
        }).present();
      }
    };
  })();

  /**
   * 统一调用此方法显示loading
   * @param content 显示的内容
   */
  showLoading(content = ''): void {
    if (this.loading) {// 如果loading已经存在则不再打开
      return;
    }
    const loading = this.loadingCtrl.create({
      content
    });
    loading.present();
    this.loading = loading;
  }

    /**
   * 关闭loading
   */
  hideLoading(): void {
    this.loading && this.loading.dismiss();
    this.loading = null;
  }

    /**
   * 统一调用此方法显示提示信息
   * @param message 信息内容
   * @param duration 显示时长
   */
  showToast(message = '操作完成', duration = 2000, position:string ="bottom" ): void {
    if (this.isMobile()) {
      this.toast.show(message, String(duration), position).subscribe();
    } else {
      this.toastCtrl.create({
        message,
        duration,
        position: position,
        showCloseButton: false
      }).present();
    }
  }
      /**
   * 获取网络类型 如`unknown`, `ethernet`, `wifi`, `2g`, `3g`, `4g`, `cellular`, `none`
   */
  getNetworkType(): string {
    if (!this.isMobile()) {
      return 'wifi';
    }
    return this.network.type;
  }

  /**
   * 判断是否有网络
   */
  isConnecting(): boolean {
    return this.getNetworkType() != 'none';
  }
  getVersionNumber(): Promise<string> {
    return new Promise((resolve) => {
      this.appversion.getVersionNumber().then((value: string) => {
        resolve(value);
      }).catch(err => {
        console.log('getVersionNumber:' + err);
      });
    });
  }
  openUrlByBrowser(url: string): void {
    this.inAppBrowser.create(url, '_system');
  }
}
